package xyz.xpman.prisonerfitness.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import java.util.Objects;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.activity.base.BaseActivity;
import xyz.xpman.prisonerfitness.entity.User;
import xyz.xpman.prisonerfitness.service.UserService;
import xyz.xpman.prisonerfitness.util.ToastUtil;

@RequiresApi(api = Build.VERSION_CODES.O)
public class UserIndexActivity extends BaseActivity {
    private UserService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_index);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        service = UserService.ME;

        init();
    }


    private void init() {
        TextView id = findViewById(R.id.id);
        TextView sex = findViewById(R.id.sex);
        TextView username = findViewById(R.id.username);


        User user = service.getCurrentUser();

        if (null == user) {
            toActivity(LoginActivity.class);
            ToastUtil.showText(this, "非法登录请求!");
            return;
        }



        // service.getCurrentUserId();
        Integer userId = user.getId();
        id.setText(String.valueOf(userId));
        sex.setText(user.getSex());
        username.setText(user.getUsername());


        // 手动同步数据
        findViewById(R.id.sync).setOnClickListener(view-> {
            // TODO: 2021/5/28 同步数据
        });


        // 登出
        findViewById(R.id.logout).setOnClickListener(view-> {
            if (service.logout()) {
                finish();
                return;
            }

            ToastUtil.showText(this, "登出失败!");
        });

    }




    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return processMenuItemForBack(item);
    }

}